'use strict'

define([ 'jquery', 'bootstrap', 'select2', 'shiftform', 'validate',
    'validate.en'], function($) {
  return {
    init : function() {
      
      this.targetElement = null;
      this.targetElementCount = "";
      this.initPage();
      this.initChooseCount();
      this.initSaveSelect2Val();
      this.submitForm();
    },
    
    initPage : function() {
     
      $('.select2').select2({
          minimumResultsForSearch: -1,
          allowClear: true
       });
      
      $(document).on("click", ".reference-btn", function() {
        $(".left-edit-wrap").toggleClass("transition")
        $(".reference-notes-wrap").toggleClass("transition")
      })
      
      $('a[data-toggle="tab"]').on('hide.bs.tab', function (e) {
        $(".left-edit-wrap").removeClass("transition")
        $(".reference-notes-wrap").removeClass("transition")
      })
      
      $('#achievement-submit').click(function() {
    	  $('a[href="#change"]').trigger("click"); 
      })
      
      $('#change-submit').click(function() {
    	  $('a[href="#development"]').trigger("click"); 
      })
      
      $('#development-submit').click(function() {
    	  $('a[href="#cooperation"]').trigger("click"); 
      })
      
      $('#cooperation-submit').click(function() {
    	  $('a[href="#management"]').trigger("click"); 
      })
    },
    
    initChooseCount: function() {
      var that = this;
      $(document).on("click", ".open-modal", function(e) {
        $($(e.currentTarget).data("modal")).modal();
        that.targetElement = $(e.currentTarget);
        if($(e.currentTarget).data("modal") == "#chooseCountModal") {
          that.targetElementCount = $(e.currentTarget).html();
        }
      })
      
      $(document).on("hidden.bs.modal", ".modal", function() {
        that.targetElement = null;
      })
      
    },
    
    initSaveSelect2Val: function() {
      var that = this;
      
      $("#chooseDelegate .save-change").on("click", function() {
        var delegateValue = $("select[name='delegate-option']").val()
        $(".choose-delegate span").html("delegate"+delegateValue);
        window.location.href = window.__CTX__.root + "/sessions/1/report-finalizing/change-delegate?delegateId=" + delegateValue;
      })
      
      $("#globalScoring .save-change").on("click", function() {
        var globalScoring = $("select[name='count']").val();
        $(".global-scoring").html(globalScoring);
        $("#globalScoring").modal("hide")
      })
      
      $("#chooseStrength .save-change").on("click", function(e) {
        var chooseStrength = $("select[name='chooseStrength']").val();
        if(chooseStrength == "strength") {
          that.targetElement.removeClass().addClass("open-modal strength")
        } else {
          that.targetElement.removeClass().addClass("open-modal development-needs")
        }
        that.targetElement.html("top "+ chooseStrength);
        $("#chooseStrength").modal("hide")
      })
      
      $("#chooseCountModal .save-change").on("click", function() {
        var scoreNum = parseInt($("select[name='score']").val())
        var original = that.targetElement.parents("tr").find("div[data-option='" +that.targetElementCount + "']").parent().data("count")
        
        that.targetElement.html(scoreNum);
        if(parseInt(that.targetElementCount) != scoreNum) {
          var originalClass = "color-level-" + original
          var colorLevel = "color-level-"+ (original-1)
          that.targetElement.parents("tr").find("div[data-option='"+ that.targetElementCount +"']").parent().removeClass(originalClass).addClass(colorLevel);
          that.targetElement.parents("tr").find("div[data-option='"+ that.targetElementCount +"']").parent("td").data("count", parseInt(original-1))
          that.targetElement.parents("tr").find("div[data-option='"+ that.targetElementCount +"']").next(".count").html((original-1))
          
          var basicsNum =  that.targetElement.parents("tr").find('[data-option="'+ scoreNum +'"]').parent().data("count");
          var basicsColorValue = "color-level-" + scoreValue
          var scoreValue = "color-level-" + (basicsNum+1)
          
          that.targetElement.parents("tr").find("div[data-option='"+ scoreNum +"']").parent().removeClass(basicsColorValue).addClass(scoreValue).data("count", parseInt(basicsNum+1))
          that.targetElement.parents("tr").find("div[data-option='"+ scoreNum +"']").next(".count").html(basicsNum+1)
        }
        
        $("#chooseCountModal").modal("hide")
      })
    },
    
    submitForm: function() {
      $(document).find('form[data-validate="true"]').bt_validate()  //bind validate
      
      var $form = $(document).find("form")                          //bind shiftform
      $form.shiftform({
        beforeSubmit : function(el) {
          var strengthLength = $(".strength").length;
          var developmentLength = $(".development-needs").length;
          var globalScoring = $(".global-scoring").html();
          if(!((strengthLength < developmentLength) && (globalScoring == "C" || globalScoring == "S"))) {
            return false
          } else if(!((strengthLength > developmentLength) && (globalScoring == "C" || globalScoring == "DN"))) {
           return false;
          }
          
          $form.trigger("validate.ajax.submit");
          return $.bt_validate.result[$.bt_validate.form_id]
        }
      })
    }

  }
})
